/**
 * tablefilter
 * @description
 *		filter a table based on input from a text field
 */

foo.mod.tablefilter = {
	init: function(){
		foo.log('foo.tablefilter.init');
		var $ = jQuery;
		$('.tablefilter').each(function(i){

			var $module = $(this).parents('div.module:first');
			var $table = $('table:first', $module);
			var $rows = $('tbody tr',$table);

			if ($rows.length < 5){
				return; // need to change the minimum value to final row count
			} else {
				$(this).html(
					'<div class="table-filter">'+
						'<div class="filter-input">'+
							'<label for="filter-input'+i+'" class="input-hint">Filter</label>'+
							'<div class="clear"></div>'+
							'<input type="text" id="filter-input'+i+'" name="filter-input'+i+'" value="" />'+
						'</div>'+
					'</div>'
				);
			}

			$('select#update-status',$module).change(function() {

				// filter based on SELECT option value- NOTE: the terms need to be updated if copy edits are made!
				if ($(this).val()=='Show all') $rows.each(function() {$(this).show()});
				if ($(this).val()=='Show only edited') filter($rows,'Edited!');
				if ($(this).val()=='Show only not yet edited') filter($rows,'Not yet');

				// reset zebra striping
				$("tbody > tr:visible",$table)
					.filter(':even')
					.removeClass('odd')
					.addClass('even')
					.end()
					.filter(':odd')
					.removeClass('even')
					.addClass('odd');

				// trigger tableSizeIE custom event defined in foo.tablesorter.js
				$table.trigger('tableSizeIE');
			});

			$('.tablefilter input',$module).keyup(function(e){
				// if esc is pressed or nothing is entered
				if (e.keyCode == 27 || $(this).val() == '') {
					
					// hide clear button
					$('.clear').fadeOut('slow'); // show clear button
					
					//if esc is pressed we want to clear the value of search box
					$(this).val('');
					
					//we want each row to be visible because if nothing is entered then all rows are matched.
					$rows.show();
				
				} else {
					
					// if there is text, lets filter
					filter($rows, $(this).val());

					// show clear button
					$('.clear',$module).fadeIn('slow');

					var field = $(this);
					
					// onclick, clear field
					$('.clear',$module).click(function(){
						field.val('');
						$(this).fadeOut('slow');
						filter($rows, $(this).val());
						return false;
					});
				}

				// reset zebra striping
				$("tbody > tr:visible",$table)
					.filter(':even')
					.removeClass('odd')
					.addClass('even')
					.end()
					.filter(':odd')
					.removeClass('even')
					.addClass('odd');

				// trigger tableSizeIE custom event defined in foo.tablesorter.js
				$table.trigger('tableSizeIE');
			});

			// filter results based on query
			function filter(rows, query) {
				query = $.trim(query); // trim white space
				query = query.replace(/ /gi, '|'); // add OR for regex query
				$(rows).each(function() {
					($(this).text().search(new RegExp(query, "i")) < 0) ? $(this).hide() : $(this).show();
				});
			}
		});
	}
};